﻿// This file is part of GEOTRON.

// GEOTRON is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The GEOTRON library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with GEOTRON.  If not, see <http://www.gnu.org/licenses/>.

using System.Diagnostics;
using System;

namespace Geotron
{
    /// <summary>
    /// Describes the result achieved by the resolution function.
    /// </summary>
    public enum Result
    {
        CannotResolve, Inspecific, Resolved
    }

    /// <summary>
    /// Describes whether the co-ordinates class is being accessed using degrees-minutes-seconds, signed decimal degress or radians.
    /// </summary>
    public enum AngleType
    {
        DMS, Degrees, Radians
    }

    /// <summary>
    /// Describes a point on the Earth's surface.
    /// </summary>
    public class Location
    {
        private double _Latitude;
        private double _Longitude;

        /// <summary>
        /// Sets the current conversion applied to setting angles.
        /// </summary>
        public AngleType Mode = AngleType.Radians;

        /// <summary>
        /// Fetch or set the current latitude using the current angle system.
        /// </summary>
        public double Latitude
        {
            get 
            {
                if(Mode == AngleType.Degrees) 
                    return _Latitude*(180/Math.PI);
                else if(Mode == AngleType.Radians)
                    return _Longitude;
                else 
                {
                    throw new Exception("DMS return is not supported.");
                }
            }

            set 
            {
                if(Mode== AngleType.Degrees) _Latitude = value*(Math.PI/180);
                else if (Mode == AngleType.Radians) _Latitude = value;
                else throw new NotImplementedException("DMS support not yet ready.");
            }
        }

        /// <summary>
        /// Fetch or set the current latitude using the current angle system.
        /// </summary>
        public double Longitude
        {
            get
            {
                if (Mode == AngleType.Degrees)
                    return _Longitude * (180 / Math.PI);
                else if (Mode == AngleType.Radians)
                    return _Longitude;
                else
                {
                    throw new Exception("DMS return is not supported.");
                }
            }

            set
            {
                if (Mode == AngleType.Degrees) _Longitude = value * (Math.PI / 180);
                else if (Mode == AngleType.Radians) _Longitude = value;
                else throw new NotImplementedException("DMS support not yet ready.");
            }
        }

    }

    /// <summary>
    /// Describes the result of the location resolution.
    /// </summary>
    public class LocationResult
    {
        public Result Result;
        public int Certainty;
        public Location Coordinates;
        public bool isValid {set;get;}
        public string Copyright;

        public LocationResult(Result ResultArg, int CertaintyArg) 
        {
            new LocationResult(ResultArg, CertaintyArg, new Location(), false);
        }

        public LocationResult(Result ResultArg, int CertaintyArg, Location LocationArg)
        {
            new LocationResult(ResultArg, CertaintyArg, LocationArg, true);
        }

        public LocationResult(Result ResultArg, int CertaintyArg, Location LocationArg, bool Valid)
        {
            isValid = Valid;
            Result = ResultArg; Certainty = CertaintyArg; Coordinates = LocationArg;

            if (ResultArg == Geotron.Result.CannotResolve) Trace.WriteLine("**Could not resolve the address.");
            else if (ResultArg == Geotron.Result.Inspecific) Trace.WriteLine("**Address wasn't specific enough to give a location.");
            else Trace.WriteLine("**Address traced.");

        }
    }
}